package com.itheima.service;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

// 品牌业务类
public class BrandService {
    // 查询所有品牌的方法

    /**
     * 查询所有品牌
     * @return 品牌列表
     */
    public List<Brand> findAll(){
        // 操作mybatis,查询不用管事务，增删改才提交事务
        SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession();
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brandList = brandMapper.selectAll();
        sqlSession.close();
        return brandList;
    }

    // 写方法三要素：
    public boolean addPinPai(Brand brand){
        // 操作mybatis,是否需要管事务,true自动提交事务
        SqlSession sqlSession = SqlSessionFactoryUtils.
                getSqlSessionFactory().openSession(true);
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        Integer insertFlag = brandMapper.insert(brand);
        // 如果前面是false，才commit
        //sqlSession.commit();
        sqlSession.close();
        return insertFlag>0?true:false;

    }
}
